#!/bin/bash

source config.mosquitto

if [ "$USE_MOSQUITTO" != "1" ]; then
    exit
fi

SCRIPT_DIR=$(cd `dirname $0` && pwd)
cd $SCRIPT_DIR

cd "${BUILD_FOLDER}" || exit 1

export CXX=g++

make clean
make -j$(nproc) \
    CROSS_COMPILE=arm-openwrt-linux- \
    CC=gcc \
    AR=ar \
    ARCH=arm \
    CFLAGS="-Os -mcpu=cortex-a7 -mfpu=neon-vfpv4 -I/opt/yi/toolchain-sunxi-musl/toolchain/include -L/opt/yi/toolchain-sunxi-musl/toolchain/lib" \
    WITH_SRV=no \
    WITH_UUID=no \
    WITH_WEBSOCKETS=no \
    WITH_TLS=no \
    WITH_TLS_PSK=no \
    WITH_CJSON=no \
    WITH_BRIDGE=no \
    WITH_MEMORY_TRACKING=no \
    WITH_DOCS=no \
    || exit 1

mkdir -p ../_install/bin || exit 1
mkdir -p ../_install/lib || exit 1

cp ./client/mosquitto_pub ../_install/bin || exit 1
cp ./client/mosquitto_sub ../_install/bin || exit 1

cp ./lib/libmosquitto.so.1 ../_install/lib || exit 1

arm-openwrt-linux-strip ../_install/bin/* || exit 1
arm-openwrt-linux-strip ../_install/lib/* || exit 1
